package com.lpy.book.service;

import com.lpy.book.enums.BookStatusEnums;
import com.lpy.book.mapper.BookInfoMapper;
import com.lpy.book.model.BookInfo;
import com.lpy.book.model.PageRequest;
import com.lpy.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

//     添加图书
    public void insertBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

//     显示列表
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        // 1. 拿到总数
        Integer count = bookInfoMapper.count();

        // 2. 查询当前页的数据
        List<BookInfo> bookInfos = bookInfoMapper.queryListByPage(pageRequest);


        for (BookInfo bookInfo : bookInfos) {
            // 根据状态,设置是否能借阅
            bookInfo.setStateCN(BookStatusEnums.getDescByCode(bookInfo.getStatus()).getDesc());
        }

        // 3. 返回结果
        return new PageResult<>(bookInfos, count, pageRequest);
    }

//     根据 id 查询图书
    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

//    更新图书
    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

//    批量删除图书
    public Integer batchDelete(List<Integer> list) {
        return bookInfoMapper.batchDelete(list);
    }


}
